package com.balisong.y.test;

import com.alibaba.fastjson.JSON;
import com.balisong.y.SkuApplication;
import com.balisong.y.pojo.Sku;
import com.balisong.y.service.SkuSercice;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Before;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@SpringBootTest(classes = SkuApplication.class)
public class testEsDemo {
    private RestHighLevelClient client;

    @Resource
    private SkuSercice skuSercice;

    //初始话,建立es连接客户端
    @Before
    public void init() {
        client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.200.150", 9200, "http")));
        System.out.println(client);
    }

    @Test
    public void testSer() {
        List<Sku> list = skuSercice.list();
        System.out.println(list);
    }
    //创建索引库
    @Test
    public void createIndexRequest() throws IOException {
        client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.200.150", 9200, "http")));
        CreateIndexRequest indexRequest = new CreateIndexRequest("sku");
        indexRequest.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 1));
        indexRequest.mapping("{\n" +
                "  \"properties\": {\n" +
                "    \"id\": {\n" +
                "      \"type\": \"keyword\",\n" +
                "      \"index\": \"true\"\n" +
                "    },\n" +
                "    \"name\": {\n" +
                "      \"type\": \"text\",\n" +
                "      \"index\": \"true\",\n" +
                "      \"analyzer\":\"ik_max_word\",\n" +
                "      \"store\":\"true\"\n" +
                "    },\n" +
                "    \"price\": {\n" +
                "      \"type\": \"integer\",\n" +
                "      \"index\": \"true\",\n" +
                "      \"store\":\"true\"\n" +
                "    },\n" +
                "    \"num\":{\n" +
                "    \t\"type\":\"integer\",\n" +
                "      \"index\":\"false\",\n" +
                "      \"store\":\"true\"\n" +
                "    },\n" +
                "    \"image\":{\n" +
                "    \t\"type\":\"keyword\",\n" +
                "      \"index\":\"false\",\n" +
                "      \"store\":\"true\"\n" +
                "    },\n" +
                "    \"category_id\":{\n" +
                "    \t\"type\":\"integer\",\n" +
                "      \"index\":\"true\",\n" +
                "      \"store\":\"true\"\n" +
                "    },\n" +
                "    \"category_name\":{\n" +
                "    \t\"type\":\"text\",\n" +
                "      \"index\":\"true\",\n" +
                "      \"analyzer\":\"ik_max_word\",\n" +
                "      \"store\":\"true\"\n" +
                "    },\n" +
                "    \"brand_name\":{\n" +
                "    \t\"type\":\"text\",\n" +
                "      \"index\":\"true\",\n" +
                "      \"store\":\"true\"\n" +
                "    }\n" +
                "  }\n" +
                "}", XContentType.JSON);
        CreateIndexResponse response = client.indices().create(indexRequest, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
        BulkRequest bulkRequest = new BulkRequest();
        List<Sku> skus = skuSercice.list();
        for (Sku sku : skus) {
            bulkRequest.add(new IndexRequest("sku")
                    .id(sku.getId())
                    .source(JSON.toJSONString(sku), XContentType.JSON));
        }
        BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(responses.status());

    }
    @Test
    public  void testAddDocument() {

    }

}
